package com.sleep.psy.external_server.business.evaluation.controller;


import com.alibaba.fastjson.JSON;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubGeneralScaleReq;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubQuestionReq;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubSleepReq;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubSleepScaleReq;
import com.sleep.psy.external_server.business.evaluation.service.EvaluationRecordService;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultEnum;
import com.sleep.psy.external_server.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 * 测评记录 前端控制器
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Api(tags = {"测评记录"})
@RestController
@RequestMapping("/evaluationRecord")
public class EvaluationRecordController {

    protected Logger logger = LoggerFactory.getLogger(EvaluationRecordController.class);

    @Autowired
    private EvaluationRecordService evaluationRecordService;

    /**
     * 提交匹兹堡量表
     * @author wenliguo
     * @date 2025/6/5 19:07
     * @param req 请求
     * @return com.sleep.psy.main_server.result.Result
     */
    @PostMapping("/subSleepScale")
    @ApiOperation(value = "提交匹兹堡量表")
    public Result<Long> subSleepScale(@Valid @RequestBody SubSleepScaleReq req) {

        logger.info("提交匹兹堡量表参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subSleepScale(req);
        } catch (Exception e) {
            logger.error("提交匹兹堡量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 提交通用量表
     * @author wenliguo
     * @date 2025/7/23 10:25
     * @param req 请求
     * @return com.sleep.psy.main_server.result.Result<java.lang.Long>
     */
    @PostMapping("/subGeneralScale")
    @ApiOperation(value = "提交通用量表")
    public Result<Long> subGeneralScale(@Valid @RequestBody SubGeneralScaleReq req) {

        logger.info("提交通用量表参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subGeneralScale(req);
        } catch (Exception e) {
            logger.error("提交通用量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 失眠严重指数量表
     * @author wenliguo
     * @date 2025/7/9 17:07
     * @param req 请求
     * @return com.sleep.psy.main_server.result.Result<java.lang.Long>
     */
    @PostMapping("/subIsi")
    @ApiOperation(value = "提交失眠严重指数量表")
    public Result<Long> subIsi(@Valid @RequestBody SubSleepScaleReq req) {

        logger.info("提交失眠严重指数量表参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subIsi(req);
        } catch (Exception e) {
            logger.error("提交失眠严重指数量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 提交睡眠问卷
     * @author wenliguo
     * @date 2025/6/6 14:53
     * @param req 请求
     * @return com.sleep.psy.main_server.result.Result<java.lang.Long>
     */
    @PostMapping("/subSleepQuestion")
    @ApiOperation(value = "提交睡眠问卷")
    public Result<Long> subSleepQuestion(@Valid @RequestBody SubQuestionReq req) {

        logger.info("提交睡眠问卷参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subSleepQuestion(req);
        } catch (Exception e) {
            logger.error("提交睡眠问卷出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 通过用户id判断用户是否答过失眠问卷和睡眠问卷
     * @author wenliguo
     * @date 2025/8/27 17:21
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.lang.Long>
    */
    @GetMapping("/isAnswerQuestion")
    @ApiOperation(value = "通过用户id判断用户是否答过失眠问卷和睡眠问卷")
    public Result<HashMap<String, Boolean>> isAnswerQuestion(@RequestParam("userId") Long userId) {

        logger.info("通过用户id判断用户是否答过失眠问卷和睡眠问卷参数:{}", userId);
        try {
            return evaluationRecordService.isAnswerQuestion(userId);
        } catch (Exception e) {
            logger.error("通过用户id判断用户是否答过失眠问卷和睡眠问卷出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过用户id获取失眠问卷记录
     * @author wenliguo
     * @date 2025/8/27 17:33
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.util.HashMap<java.lang.String,java.lang.Boolean>>
    */
    @GetMapping("/selectSleeplessRecordByUserId")
    @ApiOperation(value = "通过用户id获取失眠问卷记录")
    public Result<HashMap<String, Boolean>> selectSleeplessRecordByUserId(@RequestParam("userId") Long userId) {

        logger.info("通过用户id获取失眠问卷记录参数:{}", userId);
        try {
            return evaluationRecordService.selectSleeplessRecordByUserId(userId);
        } catch (Exception e) {
            logger.error("通过用户id获取失眠问卷记录出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过量表id和用户id判断用户是否答过该量表
     * @author wenliguo
     * @date 2025/8/28 09:48
     * @param scaleId 量表id
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.lang.Long>
    */
    @GetMapping("/isAnswer")
    @ApiOperation(value = "通过量表id和用户id判断用户是否答过该量表")
    public Result<Long> isAnswer(@RequestParam("scaleId") Integer scaleId, @RequestParam("userId") Long userId) {

        logger.info("通过量表id和用户id判断用户是否答过该量表参数:{}", userId);
        try {
            return evaluationRecordService.isAnswer(scaleId, userId);
        } catch (Exception e) {
            logger.error("通过量表id和用户id判断用户是否答过该量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 提交失眠问卷
     * @author wenliguo
     * @date 2025/8/28 15:33
     * @param req 请求
     * @return com.sleep.psy.external_server.result.Result<java.lang.Long>
    */
    @PostMapping("/subSleepLessScale")
    @ApiOperation(value = "提交失眠问卷")
    public Result<Long> subSleepLessScale(@Valid @RequestBody SubGeneralScaleReq req) {

        logger.info("提交失眠问卷参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subSleepLessScale(req);
        } catch (Exception e) {
            logger.error("提交失眠问卷出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 判断是否答过睡眠评估
     * @author wenliguo
     * @date 2025/9/8 11:05
     * @param req 请求
     * @return com.sleep.psy.external_server.result.Result<java.lang.Long>
    */
    @PostMapping("/subSleep")
    @ApiOperation(value = "判断是否答过睡眠评估")
    public Result<Boolean> subSleep(@Valid @RequestBody SubSleepReq req) {

        logger.info("判断是否答过睡眠评估参数:{}", JSON.toJSONString(req));
        try {
            return evaluationRecordService.subSleep(req);
        } catch (Exception e) {
            logger.error("判断是否答过睡眠评估出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

}

